﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace WindowsGame1
{
    class Objeto2D
    {
        private Texture2D textura;
        public Texture2D Textura
        {
            get { return textura; }
        }
        private Vector2 posicao;
        public Vector2 Posicao
        {
            get { return posicao; }
            set { posicao = value; }
        }
        private Color cor;
        public Color Cor
        {
            get { return cor; }
            set { cor = value; }
        }
        private Rectangle retangulo;
        public Rectangle Retangulo
        {
            get { return retangulo; }
            set { retangulo = value; }
        }

        private float rotacao;
        public float Rotacao
        {
            get { return rotacao; }
            set { rotacao = value; }
        }

        private Vector2 origem;
        public Vector2 Origem
        {
            get { return origem; }
            set { origem = value; }
        }

        private Vector2 escala;
        public Vector2 Escala
        {
            get { return escala; }
            set { escala = value; }
        }

        private SpriteEffects efeito;
        public SpriteEffects Efeito
        {
            get { return efeito; }
            set { efeito = value; }
        }

        private float camada;
        public float Camada
        {
            get { return camada; }
            set { camada = value; }
        }


        public Objeto2D(Vector2 posicao, Color cor, Texture2D textura)
        {
            this.posicao = posicao;
            this.cor = cor;
            this.textura = textura;
            this.retangulo = new Rectangle(0, 0, textura.Width, textura.Height);
            this.rotacao = 0f;
            this.origem = Vector2.Zero;//new Vector2(textura.Width / 2, textura.Height / 2);
            this.efeito = SpriteEffects.None;
            this.camada = 0f;
            this.escala = new Vector2(1);
        }

        public Rectangle calculaRectangulo(Vector2 position, Rectangle rect)
        {

            return new Rectangle((int)position.X, (int)position.Y, rect.Width, rect.Height);
        }

        public bool Collision(Objeto2D obj)
        {

            bool right = this.posicao.X + this.retangulo.Width > obj.Posicao.X
                          && this.posicao.X < obj.Posicao.X;
            bool left = this.posicao.X < obj.Posicao.X + obj.Retangulo.Width
                && this.posicao.X > obj.Posicao.X + obj.Retangulo.Width;
            bool up = this.posicao.Y + this.retangulo.Height > obj.Posicao.Y
                && this.posicao.Y + this.retangulo.Height < obj.Posicao.Y;
            bool down = this.posicao.Y < obj.Posicao.Y + obj.Retangulo.Height
               && this.posicao.Y > obj.Posicao.Y + obj.Retangulo.Height;
            return (right || left) && (up || down);


        }


        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(textura, posicao, retangulo, cor, rotacao, origem, escala, efeito, camada);
        }
    }
    class AnimatedObject2D
    {
        private Texture2D textura;
        public Texture2D Textura
        {
            get { return textura; }
        }
        private Vector2 posicao;
        public Vector2 Posicao
        {
            get { return posicao; }
            set { posicao = value; }
        }
        private Color cor;
        public Color Cor
        {
            get { return cor; }
            set { cor = value; }
        }
        private Rectangle retangulo;
        public Rectangle Retangulo
        {
            get { return retangulo; }
            set { retangulo = value; }
        }

        private float rotacao;
        public float Rotacao
        {
            get { return rotacao; }
            set { rotacao = value; }
        }

        private Vector2 origem;
        public Vector2 Origem
        {
            get { return origem; }
            set { origem = value; }
        }

        private Vector2 escala;
        public Vector2 Escala
        {
            get { return escala; }
            set { escala = value; }
        }

        private SpriteEffects efeito;
        public SpriteEffects Efeito
        {
            get { return efeito; }
            set { efeito = value; }
        }

        private float camada;
        public float Camada
        {
            get { return camada; }
            set { camada = value; }
        }

        private Animation[] animations;
        public Animation[] Animations
        {
            get { return animations; }
            set { animations = value; }
        }
        private Animation currentAnimation;
        public Animation CurrentAnimation
        {
            get { return currentAnimation; }
            set { currentAnimation = value; }
        }
        public AnimatedObject2D(Vector2 posicao, Color cor, Texture2D textura, Animation[] animations)
        {
            this.posicao = posicao;
            this.cor = cor;
            this.textura = textura;
            this.retangulo = new Rectangle(0, 0, 
                (int)currentAnimation.frameSize.X, 
                (int)currentAnimation.frameSize.Y);
            this.animations = animations;
            currentAnimation = animations[0];
            this.rotacao = 0f;
            //this.origem = new Vector2(retangulo.Width / 2, retangulo.Height / 2);
            this.efeito = SpriteEffects.None;
            this.camada = 0f;
            this.escala = new Vector2(1);
        }

        public virtual void Update(GameTime gameTime)
        {
            if (currentAnimation.lastUpdate < 
                (float)gameTime.TotalGameTime.TotalMilliseconds)
            {
                currentAnimation.currentFrame.X += 1;
                if (currentAnimation.currentFrame.X >= 
                    currentAnimation.totalFrames.X)
                {
                    currentAnimation.currentFrame.X = 0;
                    currentAnimation.currentFrame.Y += 1;
                    if (currentAnimation.currentFrame.Y >=
                        currentAnimation.totalFrames.Y)
                    {
                        currentAnimation.currentFrame.Y = 0;

                    }
                }
                
                retangulo = new Rectangle(
                   (int)(currentAnimation.startP.X + currentAnimation.currentFrame.X * currentAnimation.frameSize.X),
                   (int)(currentAnimation.startP.Y + currentAnimation.currentFrame.Y * currentAnimation.frameSize.Y),
                   (int)currentAnimation.frameSize.X,
                   (int)currentAnimation.frameSize.Y);
                currentAnimation.lastUpdate = currentAnimation.frameRate +
                    (float)gameTime.TotalGameTime.TotalMilliseconds;
            }


        }

        public Rectangle calculaRectangulo(Vector2 position, Rectangle rect)
        {

            return new Rectangle((int)position.X, (int)position.Y, rect.Width, rect.Height);
        }

        public bool Collision(Objeto2D obj)
        {
            bool right = this.posicao.X + this.retangulo.Width > obj.Posicao.X
                          && this.posicao.X == obj.Posicao.X;

            bool left = this.posicao.X > obj.Posicao.X - obj.Retangulo.Width
                && this.posicao.X < obj.Posicao.X + obj.Retangulo.Width;

            bool up = this.posicao.Y - this.retangulo.Height < obj.Posicao.Y
                && this.posicao.Y + this.retangulo.Height > obj.Posicao.Y;

            bool down = this.posicao.Y > obj.Posicao.Y - obj.Retangulo.Height
               && this.posicao.Y < obj.Posicao.Y + obj.Retangulo.Height;

            return (right || left) && (up || down);
        }


        public virtual void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(textura, posicao, retangulo, cor, rotacao, origem, escala, efeito, camada);
        }
    }

    public struct Animation
    {
        public float lastUpdate;
        public float nextUpdate;
        public float frameRate;
        public Vector2 frameSize;
        public Vector2 currentFrame;
        public Vector2 totalFrames;
        public Vector2 startP;
        public String name;


    }

}